package org.dfl.core.model.transitions;

import java.util.Map;

import org.dfl.core.data.connectors.MapTypeConnector;
import org.dfl.core.data.connectors.TypeConnector;
import org.dfl.core.data.types.Type;
import org.dfl.core.data.types.TypeManager;
import org.dfl.core.model.TransitionExecutionException;
import org.dfl.messages.Messages;

/**
 * transition that creates a tuple
 * @author Piotr Wlodarczyk
 *
 */
public class TupleConstructionTransition extends MultipleInputsTransition {
	@Override
	public void addInputDefinition(String key, Type type) throws Exception {
		super.addInputDefinition(key, type);
		// connect with the output
		TypeConnector connector= new MapTypeConnector(getInputsDefinition().get(key),getOutputTypeContainer(),key);
		if(!getOutputTypeContainer().connectBy(connector)){
			getInputsDefinition().remove(key);
			throw new Exception(Messages.TupleConstructionTransition_errorInputCannotBeMapped);
		}
	}
	
	@Override
	protected int getDefaultMaxInputs() {
		return -1;
	}

	@Override
	protected int getDefaultMinInputs() {
		return 0;
	}
	
	@Override
	public void removeInputDefinition(String key) {
		getInputsDefinition().get(key).separate();
		super.removeInputDefinition(key);
	}
	
	@Override
	protected Object fire(Map<String, Object> inputMap) throws TransitionExecutionException {
		return inputMap;
	}

	@Override
	public String getDefaultDescription() {
		return Messages.TupleConstructionTransition_description;
	}

	@Override
	protected Type getDefaultOutputDefinition() {
		return TypeManager.getMapType();
	}

}
